#include "sqlutil.h"
#include <QDebug>


//必须让单例指向null，不然会报错
SqlUtil *SqlUtil::sqlUtil = nullptr;

SqlUtil::SqlUtil()
{

}

SqlUtil::~SqlUtil()
{

}

SqlUtil *SqlUtil::instance()
{
    if(sqlUtil == nullptr)
    {
        sqlUtil = new SqlUtil();
        return sqlUtil;
    }
    else
    {
        return sqlUtil;
    }
}

void SqlUtil::setDatabaseInfo(QString strDriver,QString strHostName,int port,QString strDatabaseName, QString strUserName, QString strPassWord)
{
    m_driver = strDriver;
    m_strHostName = strHostName;
    m_port = port;
    m_strDatabaseName = strDatabaseName;
    m_strUserName = strUserName;
    m_strPassWord = strPassWord;
}

void SqlUtil::setDatabaseInfo(QString strDriver,QString strHostName,QString strDatabaseName)
{
    m_driver = strDriver;
    m_strHostName = strHostName;
    m_strDatabaseName = strDatabaseName;
}



//获取QT支持的驱动
void SqlUtil::getDrivers()
{
    QStringList ls = QSqlDatabase::drivers();
    qDebug() << "当前Qt版本支持的驱动有:"<<ls;
}

//获取操作数据库的实例
void SqlUtil::initDatabase()
{
    //mysql
    //1.获取数据库操作实例
    m_db = QSqlDatabase::addDatabase(m_driver);
    //2.加载数据源
    m_db.setHostName(m_strHostName);
    m_db.setPort(m_port);   // 如果使用的是默认端口, 可以不调用该函数
    m_db.setDatabaseName(m_strDatabaseName);
    m_db.setUserName(m_strUserName);
    m_db.setPassword(m_strPassWord);

    // 3. 连接数据库
    bool bl = m_db.open();
    if(!bl)
    {
        qDebug() << m_db.lastError().text();
        return;
    }
    else
    {
        //qDebug() << "数据库连接成功了...";
    }
    // 开启事务，注意开启了事务，只要是对数据库的写（插入、删除、修改、创建表等）操作都要提交事务（调用m_db.commit();）才会执行sql语句
    //m_db.transaction();
    //提交
    //m_db.commit();
    //回滚
    //m_db.rollback();
}

void SqlUtil::closeDatabase()
{
    if(m_db.isOpen())
    {
        m_db.close();
        qDebug()<<"成功关闭数据库";
    }
    else
    {
        qDebug()<<"数据库已关闭";
    }
}

QSqlDatabase SqlUtil::getDb(){
    return m_db;
}


bool SqlUtil::executeSql(QString strSql)
{
    if(!m_db.isOpen())
    {
        //重连
        initDatabase();
        if(!m_db.isOpen())
        {
            qDebug()<<"Failed to connect to root Sql admin";
            return false;
        }
        else
        {
            qDebug()<<"successed to connect";
        }
    }
    QSqlQuery query;
    query.prepare(strSql);
    bool bExecute = query.exec();
    return bExecute;
}
bool SqlUtil::executeSql(QSqlQuery query)
{
    if(!m_db.isOpen())
    {
        initDatabase();
        if(!m_db.isOpen())
        {
            qDebug()<<"Failed to connect to root Sql admin";
            return false;
        }
        else
        {
            qDebug()<<"successed to connect";
        }
    }
    bool bExecute = query.exec();
    return bExecute;
}

bool SqlUtil::selectData(QList<QVariantList> &variantlistResult, QString strSql, int iColumn)
{
    if(!m_db.isOpen())
    {
        initDatabase();
        if(!m_db.isOpen())
        {
            qDebug()<<"Failed to connect to root Sql admin";
            return false;
        }
        else
        {
            qDebug()<<"successed to connect";
        }
    }

    QSqlQuery query;
    query.prepare(strSql);
    bool flag = query.exec();

    QVariantList variantlist;
    while(query.next())
    {
        for(int i= 0; i < iColumn; i++)
        {
            QVariant value  = query.value(i);
            variantlist.append(value);
        }
        variantlistResult.push_back(variantlist);
        variantlist.clear();
    }
    return flag;
}


